package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.QuotationEntity3;
import com.sgb.kjwl.model.entity.pick.QuotationListEntity3;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.ArrayList;
import java.util.List;

public class QuotationListAdapter3 extends BaseRecycleAdapter<List<QuotationListEntity3>> {

    private TextView title_view;
    private EditText et_price;
    private TextView tv_price;
    private RecyclerView recyclerView;
    private List<QuotationEntity3> allList = new ArrayList<>();
    private ISumPrice iSumPrice;
    private String type = "";

    public QuotationListAdapter3(Context context, List<List<QuotationListEntity3>> datas) {
        super(context, R.layout.item_list_of_quotations3, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final List<QuotationListEntity3> entity) {
        holder.setBinding(BR.viewModel, entity);
        title_view = holder.itemView.findViewById(R.id.title_view);
        et_price = holder.itemView.findViewById(R.id.et_price);
        tv_price = holder.itemView.findViewById(R.id.tv_price);
        recyclerView = holder.itemView.findViewById(R.id.item_view);

        allList.add(new QuotationEntity3(entity.get(0).getValue(), "", "1"));

        if (type.equals("4") || type.equals("2")) {
            tv_price.setText("总价(元)");
        } else {
            tv_price.setText("单价(元)");
        }
        recyclerView.setLayoutManager(new GridLayoutManager(context, 1));
        QuotationItemAdapter3 adapter3 = new QuotationItemAdapter3(context, entity.subList(1, entity.size()));
        recyclerView.setAdapter(adapter3);
        adapter3.setNumListener(new QuotationItemAdapter3.QuotationNumListener() {
            @Override
            public void setNum(String num) {
                allList.get(holder.getPosition()).setNum(num);
            }
        });

        if (holder.getAdapterPosition() == 0) {
            title_view.setVisibility(View.VISIBLE);
        }

        et_price.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                allList.get(holder.getPosition()).setPrice(editable.toString());
                iSumPrice.onSum();
            }
        });
    }

    public List<QuotationEntity3> getChangeData() {
        return allList;
    }

    public interface ISumPrice {
        void onSum();
    }

    public void ISumPrice(ISumPrice iSumPrice) {
        this.iSumPrice = iSumPrice;
    }

    public void setType(String type) {
        this.type = type;
        notifyDataSetChanged();
    }
}
